package model

import (
	"gorm.io/gorm"
	"novel-mgr1/userrpc/userrpc"
)

type Books struct {
	gorm.Model
	Uid int64 `gorm:"type:int;not null"`
	Nid int64 `gorm:"type:int;not null"`
}

func (b *Books) BooksAdd(db *gorm.DB) error {
	return db.Debug().Create(b).Error
}

func (b *Books) FindBooksById(db *gorm.DB, in *userrpc.BooksAddRequest) error {
	return db.Debug().Where("uid=?", in.Uid).Where("nid=?", in.Nid).Find(b).Error
}

func (b *Books) BooksDel(db *gorm.DB, in *userrpc.BooksDelRequest) error {
	return db.Debug().Where("uid=?", in.Uid).Where("nid=?", in.Nid).Delete(b).Error
}

func BooksList(db *gorm.DB, uid int64) []*userrpc.BooksList {
	var list []*userrpc.BooksList
	db.Debug().Model(&Books{}).
		Select("users.nickname as nick_name,novels.*").
		Joins("left join novels on novels.id = books.nid").
		Joins("left join users on novels.cid = users.id").
		Where("books.uid=?", uid).
		Find(&list)
	return list
}
